package com.kaka.bus.engine.borrow.person.step;

import android.support.v7.widget.AppCompatButton;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kaka.bus.AppCenter;
import com.kaka.bus.R;
import com.kaka.bus.base.fragment.SimpleFragment;
import com.kaka.bus.constant.UserConstant;
import com.kaka.bus.engine.borrow.person.PersonActivity;
import com.kaka.bus.manager.BottomDialogManager;
import com.kaka.bus.util.U;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import butterknife.BindView;

/**
 * Created by zhangqi on 2017/12/21.
 * e-mail : ${email}
 * desc :
 */

public class Step1Fragment extends SimpleFragment {

    @BindView(R.id.edt_self_qq)
    EditText mEdtSelfQq;
    @BindView(R.id.edt_self_email)
    EditText mEdtSelfEmail;
    @BindView(R.id.edt_self_address)
    EditText mEdtSelfAddress;
    @BindView(R.id.tv_self_edu)
    TextView mTvSelfEdu;
    @BindView(R.id.tv_self_marry)
    TextView mTvSelfMarry;
    @BindView(R.id.tv_self_soncount)
    TextView mTvSelfSoncount;
    @BindView(R.id.btn_self_commit)
    AppCompatButton mBtnSelfCommit;
    @BindView(R.id.tv_self_car)
    TextView mTvSelfCar;
    @BindView(R.id.tv_self_house)
    TextView mTvSelfHouse;

    private BottomDialogManager mEduManager;
    private BottomDialogManager mHunManager;
    private BottomDialogManager mChildManager;
    private BottomDialogManager mCarManager;
    private BottomDialogManager mHouseManager;

    private ArrayList<String> mEduList;
    private ArrayList<String> mMarryList;
    private ArrayList<String> mSonList;
    private ArrayList<String> mCarList;
    private ArrayList<String> mHouseList;
    private PersonActivity mPersonActivity;

    private LinkedHashMap<String, String> mSelfMap;

    @Override
    protected void initView(View view) {
        mPersonActivity = (PersonActivity) getActivity();
        mEduManager = new BottomDialogManager(mActivity);
        mHunManager = new BottomDialogManager(mActivity);
        mChildManager = new BottomDialogManager(mActivity);
        mCarManager = new BottomDialogManager(mActivity);
        mHouseManager = new BottomDialogManager(mActivity);
    }

    @Override
    protected void initData() {
        /**
         * 加载缓存
         */
        if (mPersonActivity != null) {
            mEdtSelfQq.setText(mPersonActivity.mPMap.get(UserConstant.QQ_NUM));
            mEdtSelfEmail.setText(mPersonActivity.mPMap.get(UserConstant.EMAIL));
            mEdtSelfAddress.setText(mPersonActivity.mPMap.get(UserConstant.USUALLYADDRESS));
            mTvSelfMarry.setText(mPersonActivity.mPMap.get(UserConstant.MARRY));
            mTvSelfEdu.setText(mPersonActivity.mPMap.get(UserConstant.EDUCATION));
            mTvSelfSoncount.setText(mPersonActivity.mPMap.get(UserConstant.GETCHILD));
//            mTvSelfCar.setText(mPersonActivity.mPMap.get(UserConstant.CAR));
//            mTvSelfHouse.setText(mPersonActivity.mPMap.get(UserConstant.HOUSE));
        }

        /**
         * 创建集合添加参数
         */
        if (mSelfMap == null) {
            mSelfMap = new LinkedHashMap<>();
        }

        /**
         * 教育相关参数的初始化
         */
        if (mEduList == null) {
            mEduList = new ArrayList<>();
            mEduList.add("小学");
            mEduList.add("初中");
            mEduList.add("高中");
            mEduList.add("大专");
            mEduList.add("本科");
            mEduList.add("研究生");
            mEduList.add("博士");
            mEduManager.setData(mEduList);
        }

        /**
         * 婚姻相关参数的初始化
         */
        if (mMarryList == null) {
            mMarryList = new ArrayList<>();
            mMarryList.add("未婚");
            mMarryList.add("已婚");
            mMarryList.add("离异");
            mMarryList.add("丧偶");
            mMarryList.add("再婚");
            mMarryList.add("复婚");
            mHunManager.setData(mMarryList);
        }


        /**
         * 子女数相关参数的初始化
         */
        if (mSonList == null) {
            mSonList = new ArrayList<>();
            mSonList.add("0");
            mSonList.add("1");
            mSonList.add("2");
            mSonList.add("3");
            mSonList.add("4个及以上");
            mChildManager.setData(mSonList);
        }

        if (mCarList == null) {
            mCarList = new ArrayList<>();
            mCarList.add("是");
            mCarList.add("否");
            mCarManager.setData(mCarList);
        }

        if (mHouseList == null) {
            mHouseList = new ArrayList<>();
            mHouseList.add("有房无贷");
            mHouseList.add("有房有贷");
            mHouseList.add("与亲戚同住");
            mHouseList.add("集体宿舍");
            mHouseList.add("租房");
            mHouseList.add("其他");
            mHouseManager.setData(mHouseList);
        }
    }

    @Override
    protected void initListener() {
        mTvSelfEdu.setOnClickListener(v -> mEduManager.show());
        mTvSelfMarry.setOnClickListener(v -> mHunManager.show());
        mTvSelfSoncount.setOnClickListener(v -> mChildManager.show());
        mTvSelfCar.setOnClickListener(v -> mCarManager.show());
        mTvSelfHouse.setOnClickListener(v -> mHouseManager.show());

        mEduManager.setOkListener(select -> mTvSelfEdu.setText(select));
        mHunManager.setOkListener(select -> mTvSelfMarry.setText(select));
        mChildManager.setOkListener(select -> mTvSelfSoncount.setText(select));
        mCarManager.setOkListener(select -> mTvSelfCar.setText(select));
        mHouseManager.setOkListener(select -> mTvSelfHouse.setText(select));

        mBtnSelfCommit.setOnClickListener(v -> {
            /**
             * 检查信息
             */
            if (check(true)) {
                mPersonActivity.stepContinue(0);
            }
        });

    }

    public boolean check(boolean flag) {
        //1.0.2 自定义异常
        if (mEdtSelfQq == null) {
            MobclickAgent.reportError(AppCenter.getInstance(), new IllegalArgumentException(flag ? "不缓存" : "缓存"));
        }

        String qq = getText(mEdtSelfQq);
        String email = getText(mEdtSelfEmail).trim();
        String address = getText(mEdtSelfAddress);
        String edu = getText(mTvSelfEdu);
        String marry = getText(mTvSelfMarry);
        String childCount = getText(mTvSelfSoncount);
        String car = getText(mTvSelfCar);
        String house = getText(mTvSelfHouse);

        /**
         * 是否需要检查参数
         */
        if (flag) {
            if (TextUtils.isEmpty(qq)) {
                showMessage("请填写QQ号码!");
                return false;
            }

            if (!U.isEmail(email)) {
                showMessage("请填写正确的邮箱!");
                return false;
            }

            if (TextUtils.isEmpty(address)) {
                showMessage("请填写常住地址!");
                return false;
            }

            if (TextUtils.isEmpty(edu)) {
                showMessage("请选择教育程度!");
                return false;
            }

            if (TextUtils.isEmpty(marry)) {
                showMessage("请选择婚姻状况!");
                return false;
            }

            if (TextUtils.isEmpty(childCount)) {
                showMessage("请选择子女数量!");
                return false;
            }

//            if(TextUtils.isEmpty(car)){
//                showMessage("请选择是否购车!");
//                return false;
//            }
//
//            if(TextUtils.isEmpty(house)){
//                showMessage("请选择住房条件!");
//                return false;
//            }
        }


        /**
         * 创建集合添加参数
         */
        if (mSelfMap != null) {
            mSelfMap.put(UserConstant.QQ_NUM, qq);
            mSelfMap.put(UserConstant.EMAIL, email);
            mSelfMap.put(UserConstant.USUALLYADDRESS, address);
            mSelfMap.put(UserConstant.MARRY, marry);
            mSelfMap.put(UserConstant.EDUCATION, edu);
            mSelfMap.put(UserConstant.GETCHILD, childCount);
//            mSelfMap.put(UserConstant.CAR, car);
//            mSelfMap.put(UserConstant.HOUSE, house);
        }
        if (mPersonActivity != null) {
            mPersonActivity.mPMap.putAll(mSelfMap);
        }
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_self;
    }

}
